package cn.itcast.controller;

import cn.itcast.domain.Book;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

//表现层
@Controller
@RequestMapping("/book")
public class HelloController {
    @RequestMapping(path = "/demo")
    public String demo() {
        System.out.println("进来了");
        //返回jsp;
        return "book/success";
    }

    //路径要写清楚;
    @RequestMapping(path = "/demo1")
    public String demo1(String username, int age) {
        System.out.println("进来了demo1");
        System.out.println(username);
        System.out.println(age);
        //返回jsp;
        return "book/success";
    }


    @RequestMapping(value = "/demo2", method = RequestMethod.POST)
    public String demo2(Book book) {
        System.out.println("进来了demo2");
        System.out.println(book);

        //返回jsp;
        return "book/success";
    }

    /**
     * 自定义类型转换器
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/demo3", method = RequestMethod.POST)
    public String demo3(Book book) {
        System.out.println("执行了demo3");
        System.out.println(book);

        //返回jsp;
        return "book/success";
    }

    @RequestMapping("/demo4")
    public String demo4(@DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        System.out.println("进来了demo4");
        System.out.println(birthday);
        //返回jsp;
        return "book/success";
    }

    /**
     * 原生的API
     *
     * @return
     */
    @RequestMapping("/demo5")
    public String demo5(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("执行了...");
        System.out.println(request);

        HttpSession session = request.getSession();
        System.out.println(session);

        ServletContext servletContext = session.getServletContext();
        System.out.println(servletContext);

        System.out.println(response);
        return "book/success";
    }

    /**
     * restful风格传值;
     *
     * @param name
     * @param age
     * @return
     */
    @RequestMapping(value = "/demo6/{name}/{age}")
    public String demo6(@PathVariable("name") String name,
                        @PathVariable("age") String age
    ) {
        System.out.println("执行了demo6");
        System.out.println(name);
        System.out.println(age);

        //返回jsp;
        return "book/success";
    }

    @RequestMapping(value = "/demo8")
    public String demo8(@CookieValue(value = "JSESSIONID") String cookieValue) {
        System.out.println("demo8");
        System.out.println(cookieValue);
        return "book/success";
       /* cookie的值
        6D32FAB56799FAB8DF75B08E4B792262*/
    }

    @RequestMapping("/demo9")
    public String demo9(String bookName) {
        System.out.println(bookName);
        //返回jsp;
        return "book/success";
    }
   //先执行
    //@ModelAttribute
    public void temp() {
        System.out.println("123");
    }
     //别名
   /* @ModelAttribute("a")*/

    @RequestMapping("/demo10")
    //MOdelMap mm
    public String demo10(Model model) {
        System.out.println("demo10");
        model.addAttribute("age",88);
        model.addAttribute("name","tom");
        System.out.println(model.asMap().get("name"));
        System.out.println(model.asMap().get("age"));
       //demo11删除所有
        // model.asMap().clear();
        /*model.asMap().remove("age");*/
        //第二种
      /*  mm.addAttribute("age",88)
        System.out.println(mm.get("age"));*/
      /* //第三种方式
        ModelAndView mav = new ModelAndView();
        mav.addObject("age",88);
        mav.getModelMap().get("age");*/
        //返回jsp;
        return "book/success";
    }
    @RequestMapping("/demo11")
    public String demo11(SessionStatus ss) {
        System.out.println("demo11");
        ss.setComplete();
        //返回jsp;
        return "book/success";
    }


}